#include "pll.h"

void chg_pll(){
    
    // PLLSETREG0 &= ~((0x3F << 18) | (0x3FF << 8) | (0xFF << 0));
    // PLLSETREG0 |= (6 << 18) | (550 << 8) | (2 << 0);    // 修改PMS
    
    PLLSETREG1 &= ~((0x3F << 18) | (0x3FF << 8) | (0xFF << 0));
    PLLSETREG1 |= (3 << 18) | (96 << 8) | (4 << 0);    // 修改PMS

    // CLKDIVREG0 &= ~((0x7 << 0) | (0x3F << 3)  | (0x3F << 9));
    // CLKDIVREG0 |= ((0 << 0) | ((2 - 1) << 3) | ((8 - 1) << 9));

    PWRMODE |= (1 << 15);           //写1表示要修改PLL值
    // CLKMODEREG0 |= (1 << 0);    // Update P,M,S values for PLL[0]
    CLKMODEREG0 |= (1 << 1);        // Update P,M,S values for PLL[1]

    while((PWRMODE >> 15) & 1);     //等PLL稳定
}

void chg_fclkdiv(){
    CLKDIVREG0 &= ~((0x3F << 3)  | (0x3F << 9));        // (0x7 << 0) | 
    CLKDIVREG0 |= (((5 - 1) << 3) | ((20 - 1) << 9));    // FCLK = 800 / 10;  HCLK = 800 / 40;
    PWRMODE |= (1 << 15); 

    while((PWRMODE >> 15) & 1);
}